/**
 * test-class of "Lagerverwaltung"
 * 
 * @author Konrad Paumann
 * @author Bernhard Denner
 * @author Maximilian Csuk 
 * 
 * $Revision: 85 $
 * $Date: 2007-10-25 06:20:31 +0000 (Thu, 25 Oct 2007) $
 *
 */
import java.util.*;

public class Test {
	
	/**
	 *  constructor of test-class.
	 */
	public Test() {
		// constructor
	}
	
	private void testUI() {
		try {
			// create a product and print it out
			Product p = new Product("SCHUH 1345", "Herrenschuh GR", 800, 20);
			UI.getInstance().display("Printing out a single product...");
			UI.getInstance().display(p);
			
			// create a productgroup and print it out
			ProductGroup g = new ProductGroup("Schuhe", 10);
			UI.getInstance().display("Printing out an empty product-group...");
			UI.getInstance().display(g); // should be empty
			
			// add the product to the group and print out the group again
			g.addProduct(p);
			UI.getInstance().display("Printing out a product-group with one product...");
			UI.getInstance().display(g);
			
			// add another product and print out the group yet again
			g.addProduct(new Product("SCHUH 6789", "Damenschuh GR", 700, 15));
			UI.getInstance().display("Printing out a product-group with two products...");
			UI.getInstance().display(g);
			
			// create a warehouse and print it out
			Warehouse w = new Warehouse();
			UI.getInstance().display("Printing out an empty warehouse...");
			UI.getInstance().display(w);
			
			// add the product-group to the warehouse and print out the warehouse again
			w.addProductGroup(g);
			UI.getInstance().display("Printing out a warehouse with one product-group...");
			UI.getInstance().display(w);	
			
			// sort
			g.addProduct(new Product("NIKE 16789", "Laufschuh", 900, 15));
			g.addProduct(new Product("ADIDAS 789", "Laufschuh ", 900, 18));
			ProductGroup h = new ProductGroup("Stifte", 10);
			h.addProduct(new Product("PARKER 449", "Parker Kuli", 200, 8));
			h.addProduct(new Product("BALLOGR189", "Ballograph Kuli", 500, 100));
			h.addProduct(new Product("JOLLY X789", "Jolly Kuli", 1400, 30));
			w.addProductGroup(h);
			// sorted by price
			UI.getInstance().display("Printing out all Products sorted by price: ");
			UI.getInstance().display(w.getSortedByPrice());
			
			// sorted by shortDescription
			UI.getInstance().display("Printing out all Products sorted by short description: ");
			UI.getInstance().display(w.getSortedByShortDescription());
			
			// sorted by description
			UI.getInstance().display("Printing out all Products sorted by description: ");
			UI.getInstance().display(w.getSortedByDescription());
			
			// sorted by amount
			UI.getInstance().display("Printing out all Products sorted by amount: ");
			UI.getInstance().display(w.getSortedByAmount());
			
			
		} catch (ProductException e) {
			e.printStackTrace();
		}
	}
	
//	private void testSerialization() {
//		try {
//			// create a new warehouse and fill it with product-groups
//			Warehouse w = new Warehouse();
//			w.addProductGroup("Shoes", 10);
//			w.addProductGroup("T-Shirts", 20);
//			
//			// add products and print out the whole warehouse
//			Product p1 = new Product("SCHUH 1345", "Herrenschuh GRAZIE", 800, 20);
//			p1.addProductUnit("Palette", 50, "Stueck");
//			p1.addProductUnit("LKW-Ladung", 20, "Palette");
//			Product p2 = new Product("TSHIRT2345", "T-Shirt Puma", 40, 120);
//			p2.addProductUnit("Karton", 100, "Stueck");
//			p2.addProductUnit("LKW-Ladung", 50, "Karton");
//			w.addProduct("Shoes", p1);
//			w.addProduct("T-Shirts", p2);
//			UI.getInstance().display("Printing out warehouse before serialization...");
//			UI.getInstance().display(w);
//			
//			// serialize the warehouse			
//			w.serialize("test.txt");
//			
//			// delete all products and the product-groups
//			UI.getInstance().display("Deleting all product-groups and products...");
//			w.removeProduct("Shoes", "SCHUH 1345");
//			w.removeProduct("T-Shirts", "TSHIRT2345");
//			w.removeProductGroup("Shoes");
//			w.removeProductGroup("T-Shirts");
//			
//			// deserialize the warehouse and print it out
//			w.deserialize("test.txt");
//			UI.getInstance().display("Printing out warehouse after deserialization...");
//			UI.getInstance().display(w); // data should be how it was when serialization took place
//			
//		} catch (ProductException e) {
//			e.printStackTrace();
//		}
//	}
	
	private void testHandler() {
		UI.getInstance().display("Creating a warehouse-manager and a new test-user");
		WarehouseManager whm = new WarehouseManager();
		User u = new User("testuser", "pw", User.WHWRITE | User.WHREAD | User.PGWRITE | User.PGREAD);
		
		// get a warehouse-handler and add a product-group
		UI.getInstance().display("Get a warehouse-handler and add a product-group");
		WarehouseHandler whh = whm.getWarehouseHandler(u);
		try {
			whh.addProductGroup("Schuhe", 20);
		} catch (ProductException e) {
			e.printStackTrace();
		} catch (UserException e) {
			e.printStackTrace();
		}
		
		// get a handler to the before created product-group and add a product
		UI.getInstance().display("Get a handler to the before created product-group and add a product");
		try
		{
			ProductGroupHandler pgh = whm.getProductGroupHandler("Schuhe", u);
			pgh.addProduct("0123456789", "Irgendwoeche Schuach hoit!", 200, 10);
		} catch (ProductException e) {
			e.printStackTrace();
		} catch (UserException e) {
			e.printStackTrace();
		}
		
		// get a handler to the before created product and get the price
		UI.getInstance().display("Get a handler to the before created product and get the price");
		try
		{
			ProductHandler ph = whm.getProductHandler("Schuhe", "0123456789", u);
			Integer price = new Integer(ph.getPrice());
			UI.getInstance().display(price.toString());
		} catch (ProductException e) {
			e.printStackTrace();
		} catch (UserException e) {
			e.printStackTrace();
		}
		
		// create another (restricted) user and try to set the price
		UI.getInstance().display("Create another (restricted) user and try to set the price");
		User u2 = new User("testuser2", "pw", User.WHREAD);
		try
		{
			ProductHandler ph = whm.getProductHandler("Schuhe", "0123456789", u2);
			ph.setPrice(300);
		} catch (ProductException e) {
			e.printStackTrace();
		} catch (UserException e) {
			e.printStackTrace();
		}
		
		// Ausgeben des logs
		UI.getInstance().display("Ausgeben des logs:");
		UI.getInstance().display(whm.getLogger());
	}
	
	private void testUnits() {
		try {
			// create a product and add some units
			Product p = new Product("SCHUH 1345", "Herrenschuh GRAZIE", 800, 20);
			p.addProductUnit("Packung", 20, "Stueck");
			p.addProductUnit("Palette", 50, "Packung");
			p.addProductUnit("LKW", 10, "Palette");
			UI.getInstance().display("Same product with units ...");
			UI.getInstance().display(p);
			
			// remove a product unit
			p.removeProductUnit("Palette");
			UI.getInstance().display("removed unit 'Palette'...");
			UI.getInstance().display(p);
			
			// add a product unit in the between to units
			p.addProductUnit("EuroPalette", 60, "Packung");
			UI.getInstance().display("add unit between to units...");
			UI.getInstance().display(p);
			
			// inc/dec product amount with use of units
			p.alterAmount(80, "Packung");
			UI.getInstance().display("add 10 'Packung' ...");
			UI.getInstance().display(p);
			p.alterAmount(-1, "EuroPalette");
			UI.getInstance().display("remove 1 'EuroPalette'...");
			UI.getInstance().display(p);
			
			
		} catch (ProductException e) {
			e.printStackTrace();
		}
		
	}
	
	public void testWarehouseManagement() {
		// testing the UI
		UI.getInstance().display("TEST UI:");
		testUI();
		
//		UI.getInstance().display("TEST SERIALIZATION:");
//		testSerialization();
		
		UI.getInstance().display("TEST PRODUCTHANDLER:");
		testHandler();
		
//		UI.getInstance().display("TEST UNITS:");
//		testUnits();
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Test t = new Test();
		t.testWarehouseManagement();
	}
}
